package com.weic.easycache.widget;

import android.content.Context;

import com.weic.easycache.framework.UserHelper;
import com.weic.easycache.phone.Network;
import com.weic.easycache.user.UserConfig;
import com.weic.easycache.utils.IdGetter;

public class Asker {
	public static final String ID_TITLE_ICO_NO_NET			= "lib_wei_c_asker_drawable_title_icon_no_net";
	public static final String ID_STR_CONTENT_NO_NET			= "lib_wei_c_asker_string_content_no_net";
	public static final String ID_STR_CONTENT_3G_ALLOWED		= "lib_wei_c_asker_string_content_3g_allowed";
	public static final String ID_STR_CONTENT_2G_ALLOWED		= "lib_wei_c_asker_string_content_2g_allowed";

	/**@param context
	 * @param showDialogIfNoNet 如果没有网络连接，是否显示设置网络对话框**/
	public static boolean checkNetStatus(final Context context, boolean showDialogIfNoNet) {
		if(!Network.isNetConnected(context)) {
			if(showDialogIfNoNet) DialogHelper.showAlertDialog(context, "网络", IdGetter.getDrawableId(context, ID_TITLE_ICO_NO_NET),
					context.getString(IdGetter.getStringId(context, ID_STR_CONTENT_NO_NET)),
					"设置", null, "取消", new Runnable() {
				@Override
				public void run() {
					Network.openNetGraceful(context);	//进入网络配置界面
				}
			}, null, null);
			return false;
		}else {
			return true;
		}
	}

	/**
	 * 询问用户网络连接情况
	 * @param allow3G		为true则3G网络下不询问
	 * @param allow2G		为true则2G网络下不询问
	 * @param doPositive
	 */
	public static void checkAndAskNetAllowed(final Context context, boolean allow3G, boolean allow2G, final Runnable doPositive) {
		if(checkNetStatus(context, true)) {
			if(Network.isWifiConnected(context) || (allow3G && Network.is3GConnected(context)) || (allow2G && Network.is2GConnected(context))) {
				doPositive.run();
				return;
			}
			UserConfig uconf = UserHelper.getUserConfig();
			if(uconf == null) uconf = new UserConfig();
			if(Network.is3GConnected(context)) {
				if(uconf.allow3gDload) {
					//不判断是否登录，未登录状态下，用户更改设置只应用于当前程序，退出后将重置
					//toast.show
					doPositive.run();
				}else {
					DialogHelper.showAlertDialog(context, "网络", IdGetter.getDrawableId(context, ID_TITLE_ICO_NO_NET),
							context.getString(IdGetter.getStringId(context, ID_STR_CONTENT_3G_ALLOWED)),
							"确定", null, "取消", doPositive, null, null);
				}
				return;
			}
			if(Network.is2GConnected(context)) {
				DialogHelper.showAlertDialog(context, "网络", IdGetter.getDrawableId(context, ID_TITLE_ICO_NO_NET),
						context.getString(IdGetter.getStringId(context, ID_STR_CONTENT_2G_ALLOWED)),
						"确定", null, "取消", doPositive, null, null);
			}
		}
	}
}
